package com.skb.task.service.impl;

import com.skb.task.mapper.ProjectMapper;
import com.skb.task.mapper.TaskMapper;
import com.skb.task.pojo.Project;
import com.skb.task.service.ProjectService;
import com.skb.task.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.skb.task.util.Constant;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;


/**
 * @Author 沈楷博
 * @Company 永遠快樂
 * @Description
 * @Time 2024/9/5 下午5:07
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TaskMapper taskMapper;
    @Override
    public QdResult queryByProjectPage(Project project, int start, int limit) {
        //查询总条数
        int total = projectMapper.selectCountProject(project);
        //查询数据列表
        List<Project> projects = projectMapper.selectByProjectPage(project, (start - 1) * limit, limit);
        for(Project project1 : projects) {
            project1.setTaskCount(taskMapper.selectCountByProjectId(project1.getProjectId()));
        }
        return QdResult.success(projects,total);
    }

    @Override
    public QdResult queryByProjectPageById(Project project, int start, int limit, Long userId) {
        //查询总条数
        int total = projectMapper.selectCountProject(project);
        //查询数据列表
        List<Project> projects = projectMapper.selectByProjectPageById(project, (start - 1) * limit, limit);
        for(Project project1 : projects) {
            project1.setTaskCount(taskMapper.selectCountByProjectId(project1.getProjectId()));
        }
        return QdResult.success(projects,total);
    }


    @Override
    public QdResult save(Project project) {
        try {
            project.setCreateTime(new Date());
            project.setModifyTime(new Date());
            // 保存项目
            int affectedRows = projectMapper.insertProject(project);
            if (affectedRows > 0) {
                return QdResult.success("项目添加成功", project.getProjectStatus());
            } else {
                return QdResult.error("项目添加失败，可能是因为数据已存在或其它原因");
            }
        } catch (Exception e) {
            return QdResult.error(e.getMessage());
        }
    }

    @Override
    public Project findProjectById(String projectId) {
        return projectMapper.findProjectById(projectId);
    }

    @Override
    public QdResult updateProject(Project project) {
        Project existingProjectByName = projectMapper.findProjectByName(project.getProjectName());
        if (existingProjectByName != null && !existingProjectByName.getProjectId().equals(project.getProjectId())) {
            return QdResult.error("项目名已存在");
        }
        int affectedRows = projectMapper.updateProject(project);
        if (affectedRows > 0) {
            return QdResult.success("项目更新成功");
        } else {
            return QdResult.error("项目更新失败");
        }
    }

    @Override
    public QdResult deleteById(String projectId) {
       Project project = new Project();
       project.setProjectId(projectId);
       project.setProjectStatus(99);
       projectMapper.updateProjectById(project);
       return QdResult.success("删除成功");
    }

    @Override
    public QdResult fileById(String projectId) {
        Project project = new Project();
        project.setProjectId(projectId);
        project.setProjectStatus(3);
        projectMapper.fileProjectById(project);
        return QdResult.success("归档成功");
    }

    @Override
    public QdResult publishProject(String[] projectIds) {
        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_PROJECT_STATUS_RELEASE));
    }

    @Override
    public QdResult archivingProject(String[] projectIds) {
        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_STATUS_ARCHIVE));
    }

    @Override
    public QdResult deletionProject(String[] projectIds) {

        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_STATUS_DELETE));
    }

    @Override
    public QdResult queryByProjectPageByPigeonhole(Project project, int start, int limit, Long userId) {
        //查询总条数
        int total = projectMapper.selectCountProject(project);
        //查询数据列表
        List<Project> projects = projectMapper.selectByProjectPageByPigeonhole(project, (start - 1) * limit, limit);
        for(Project project1 : projects) {
            project1.setTaskCount(taskMapper.selectCountByProjectId(project1.getProjectId()));
        }
        return QdResult.success(projects,total);
    }

    @Override
    public QdResult recoveryById(String projectId) {
        Project project = new Project();
        project.setProjectId(projectId);
        project.setProjectStatus(1);
        projectMapper.recoveryProjectById(project);
        return QdResult.success("恢复成功");
    }

    @Override
    public QdResult fileDeletionProject(String[] projectIds) {
        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_STATUS_DELETE));
    }

    @Override
    public QdResult filePublishProject(String[] projectIds) {
        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_PROJECT_STATUS_RELEASE));
    }

    @Override
    public QdResult queryByProjectPageByDelete(Project project, int start, int limit, Long userId) {
        //查询总条数
        int total = projectMapper.selectCountProject(project);
        //查询数据列表
        List<Project> projects = projectMapper.selectByProjectPageByDelete(project, (start - 1) * limit, limit);
        for(Project project1 : projects) {
            project1.setTaskCount(taskMapper.selectCountByProjectId(project1.getProjectId()));
        }
        return QdResult.success(projects,total);
    }

    @Override
    public QdResult deleteRecoveryProject(String[] projectIds) {
        return QdResult.success(200, "ok", projectMapper.updateStatusByIds(projectIds, Constant.DATA_PROJECT_STATUS_RELEASE));
    }

    @Override
    public QdResult queryById(String projectId) {
        return QdResult.success(projectMapper.selectById(projectId));
    }

    @Override
    public QdResult queryAllProject(String projectId) {
        return QdResult.success(projectMapper.selectByProjectNum(projectId));
    }

    @Override
    public QdResult queryMyProject(Long userId) {
        return QdResult.success(projectMapper.selectByMyProjectNum(userId));
    }
}
